﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WhittleList.Bll
{
    public partial class Site : IValidate
    {
        public DateTime Created
        {
            get { return this.UTCCreated.ToLocalTime(); }
        }

        public DateTime LastUpdated
        {
            get { return this.UTCLastUpdated.ToLocalTime(); }
        }

        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (!IsValid)
            {
                throw new Exceptions.ValidationException("Validation Errors Prevented Saving", GetRuleViolations());
            }
        }

        #region IValidate Members

        public bool IsValid
        {
            get { return GetRuleViolations().Count() == 0; }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (string.IsNullOrEmpty(Name))
                yield return new RuleViolation("Name", "Site Name must be set");
            if (string.IsNullOrEmpty(Url))
                yield return new RuleViolation("Url", "Site Url must be set");
        }

        #endregion


        internal void OnInserting(User currentUser)
        {
            _LastUpdatedById = _CreatedById = currentUser.Id;
            _UTCLastUpdated = _UTCCreated = DateTime.UtcNow;
        }

        internal void OnUpdating(User currentUser)
        {
            this._LastUpdatedById = currentUser.Id;
            this._UTCLastUpdated = DateTime.UtcNow;
        }
    }
}
